package com.sxbbc.seller.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.function.common.RestResponse;
import com.sxbbc.common.basics.rest.RestFactory;
import com.sxbbc.common.basics.utils.statics.DynamicSettingUtils;
import com.sxbbc.common.basics.utils.statics.StaticUtils;
import com.sxbbc.common.core.entity.jz.ActivityRedPacket;
import com.sxbbc.common.core.entity.jz.ActivitySeckill;
import com.sxbbc.common.core.entity.jz.ActivitySeckillDetail;
import com.sxbbc.common.core.entity.jz.ProSkuDetail;
import com.sxbbc.common.core.enums.ActivityPayEnums;
import com.sxbbc.common.core.service.*;
import com.sxbbc.common.core.utils.BasePage;
import com.sxbbc.seller.common.ActivityMapping;
import com.sxbbc.seller.util.BusinessSession;
import com.sxbbc.seller.util.SystemLog;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 活动/活动相关 控制层
 *
 * @author leehao
 * @date 2019-06-05 10:41
 */
@Controller
public class ActivityController {

    @Autowired
    private IActivityRedPacketService activityRedPacketService;
    @Autowired
    private IProSkuDetailService proSkuDetailService;
    @Autowired
    private IActivitySeckillService activitySeckillService;
    @Autowired
    private IActivitySeckillDetailService activitySeckillDetailService;
    @Autowired
    private IInfoParamService infoParamService;
    @Autowired
    private ActivityService activityService;
    @Autowired
    private IProcMainService procMainService;
    @Autowired
    private ICCustomerService icCustomerService;
    @Autowired
    private BBusinessService bBusinessService;

    /**
     * 显示商品红包活动充值页面
     * @param proCode
     * @author leehao
     * @return org.springframework.web.servlet.ModelAndView
     */
    @RequestMapping(value = ActivityMapping.SHOW_SET_RED_ACTIVITY)
    public ModelAndView showRedPacketEdit(String proCode){
        //上次充值记录
        QueryWrapper<ActivityRedPacket> wrapper = new QueryWrapper<ActivityRedPacket>();
        wrapper.eq("pro_code",proCode).eq("status",StaticUtils.STATUS_YES);
        ActivityRedPacket redPacket = activityRedPacketService.getOne(wrapper);
        //红包类型
        List<ActivityPayEnums> typeList = ActivityPayEnums.queryList();
        ModelAndView view = new ModelAndView(ActivityMapping.SHOW_SET_RED_ACTIVITY);
        view.addObject("redPacket",redPacket);
        view.addObject("proCode",proCode);
        view.addObject("typeList",typeList);
        view.addObject("canClose",DynamicSettingUtils.CAN_CLOSE_PRODUCT_RED_PACKET);
        return view;
    }

    /**
     * 秒杀活动管理
     *
     * @return org.springframework.web.servlet.ModelAndView
     * @author leehao
     */
    @RequestMapping(value = ActivityMapping.SHOW_SECKILL_LIST)
    public ModelAndView showSeckillList(BasePage<ActivitySeckill> page) {
        QueryWrapper<ActivitySeckill> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("begin_time");
        IPage<ActivitySeckill> pageInfo = activitySeckillService.page(page,wrapper);
        ModelAndView view = new ModelAndView(ActivityMapping.SHOW_SECKILL_LIST);
        view.addObject("pageInfo", pageInfo);
        return view;
    }

    /**
     * 秒杀活动列表页面
     * @param page 分页条件
     * @param id 秒杀信息的ID
     * @author leehao
     * @return org.springframework.web.servlet.ModelAndView
     */
    @RequestMapping(value = ActivityMapping.SHOW_SECKILL_DETAIL_LIST)
    public ModelAndView showSeckillDetailList(BasePage<ActivitySeckillDetail> page, Integer id) {
        ActivitySeckillDetail seckillDetail = new ActivitySeckillDetail();
        seckillDetail.setSeckillId(id);
        IPage<ActivitySeckillDetail> pageInfo = activitySeckillDetailService.queryList(page, seckillDetail);
        ModelAndView view = new ModelAndView(ActivityMapping.SHOW_SECKILL_DETAIL_LIST);
        view.addObject("pageInfo", pageInfo);
        view.addObject("seckillId", id);
        return view;
    }

    /**
     * 新增秒杀商品页面
     * @param seckillId 秒杀信息的ID
     * @author leehao
     * @return org.springframework.web.servlet.ModelAndView
     */
    @RequestMapping(value = ActivityMapping.SHOW_ADD_SECKILL_DETAIL)
    public ModelAndView showAddSeckillDetail(Integer seckillId) {
        ModelAndView view = new ModelAndView(ActivityMapping.SHOW_ADD_SECKILL_DETAIL);
        view.addObject("secKill", seckillId);
        return view;
    }

    /**
     * 查询商品详情列表
     * @param proCode 商品编码
     * @author leehao
     * @return com.function.common.RestResponse
     */
    @RequestMapping(value = ActivityMapping.QUERY_PRODUCT_DETAIL_AJAX)
    @ResponseBody
    public com.sxbbc.common.basics.rest.RestResponse queryProductDetail(HttpServletRequest request,String proCode) {
        Integer bid = BusinessSession.getBid(request);
        List<ProSkuDetail> skuList = new ArrayList<>();
        if (StringUtils.isNotBlank(proCode)) {
            skuList = proSkuDetailService.queryProductDetailList(proCode,bid);
        }
        return RestFactory.success(skuList);
    }



    /**
     * 处理秒杀活动结束后返回商品库存
     * @param id 秒杀活动的ID
     * @author leehao
     * @return com.function.common.RestResponse
     */
    @RequestMapping(value = ActivityMapping.HANDLE_SECKILL_RETURN_STOCK)
    @ResponseBody
    @SystemLog(methods = "处理秒杀商品返回库存",module = "活动管理")
    public RestResponse handleSeckillReturnStock(Integer id){
        return activitySeckillDetailService.handleSeckillReturnStock(id);
    }
}
